package streams

// ComparableStream 继承Stream接口，可以扩展comparable类型的 Stream
type ComparableStream[T comparable] interface {
	Stream[T]
}

// 接口实现检查
var _ ComparableStream[int] = (*comparableStream[int])(nil)
var _ Stream[int] = (*comparableStream[int])(nil)

type comparableStream[T comparable] struct {
	stream[T]
}

func (my *comparableStream[T]) Distinct() Stream[T] {
	list := my.list

	// 转成set
	set := make(map[T]struct{}, len(list))
	for _, t := range list {
		set[t] = struct{}{}
	}

	// 再将set转list
	newList := make([]T, 0, len(list))
	for key := range set {
		newList = append(newList, key)
	}

	my.list = newList
	return my
}
